//
//  ViewController.swift
//  6.6.Feeding Custom Cell to Collection Views Using .xib Files
//
//  Created by joker on 15/8/8.
//  Copyright (c) 2015年 joker. All rights reserved.
//

import UIKit

class ViewController: UICollectionViewController {

    
    let allImages = [
        UIImage(named: "1"),
        UIImage(named: "2"),
        UIImage(named: "3")
    ]
    
    func randomImage() -> UIImage {
        return allImages[Int(arc4random_uniform(UInt32(allImages.count)))]!
    }
    override init(collectionViewLayout layout: UICollectionViewLayout) {
        super.init(collectionViewLayout: layout)
        
        let nib = UINib(nibName: "myCollectionViewCell", bundle: nil)
        
        collectionView?.registerNib(nib, forCellWithReuseIdentifier: "cell")
        collectionView?.backgroundColor = UIColor.whiteColor()
    }
    
    convenience required init(coder aDecoder: NSCoder) {
        let flowLayout = UICollectionViewFlowLayout()
        
        flowLayout.minimumLineSpacing = 20
        
        flowLayout.minimumInteritemSpacing = 10
        
        flowLayout.itemSize = CGSize(width: 80, height: 120)
        
        flowLayout.scrollDirection = .Vertical
        
        flowLayout.sectionInset = UIEdgeInsets(top: 10, left: 20, bottom: 10, right: 20)
        
        self.init(collectionViewLayout: flowLayout)
    }
    
    override func numberOfSectionsInCollectionView(collectionView: UICollectionView) -> Int {
        return Int(3 + arc4random_uniform(4))
    }
    
    override func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        return Int(10 + arc4random_uniform(6))
    }
    
    override func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
        let cell = collectionView.dequeueReusableCellWithReuseIdentifier("cell", forIndexPath: indexPath) as! myCollectionViewCell
        cell.imageViewBackgraoudImage.image = randomImage()
        cell.imageViewBackgraoudImage.contentMode = .ScaleAspectFit
        
        
        return cell
    }
}

